// xc_lshop/business/pro/list.js
// var actbill = require('../../model/pro')
// var actbill = require('../../../xc/model/pro_contents')
var actbill = require('../model/business_pro')

Page({
  mixins: [require('../../../xc/mixins/basicMixin')],
  /**
   * 页面的初始数据
   */
  data: {
    status:0,
    nav:[
      {status:0,name:"全部"},
      {status:1,name:"在售中"},
      {status:-1,name:"已下架"},
    ],
    modellist:[],
    searchkey:""
  },
  xcloaddata: function (options, config) {
    
  },
  onClickSearch(){
    var that=this
    this.getpagelist(that,1)
  },
  del(e){
    var that=this
    var dataset=e.currentTarget.dataset;

    var idx=dataset.index
    var item = this.data.modellist[idx];
    wx.showModal({
      title: "删除",
      content: '确认删除' + '"' + item.name + '"',
      success: function (res) {
        if (res.cancel) {
          //取消删除
        } else {
          that.cop(idx,"del",dataset.proid)
        }
      }
    })
  },
  changestatus(e){
    var dataset=e.currentTarget.dataset;
    
    console.log("e")
    console.log(e)
    var data={status:dataset.status * -1}
    this.cop(dataset.index,"change",dataset.proid,data)
  },
  cop(index,op,id,data){
    var that=this;
    actbill.proop(op,id,data).then(function (result) {
      if (result["status"] == -1) {
        that.error(result['message']?result['message']:'操作失败', false)
      } else if (result["status"] == 1) {
        if(op=='del'){
          var modellist = that.data.modellist
          modellist.splice(index, 1)
          that.setData({
            modellist
          })
          wx.showToast({
            title: '删除成功',
          })
        }else{
          var item=that.data.modellist[index];
          for(var i in data){
            item[i]=data[i]
          }
          that.setData({
            ['modellist['+index+']']:item
          })
        }
        
      }
    })
  },
  showqrcode(e){
    var dataset=e.currentTarget.dataset;
    this.setData({
      proid:dataset.proid
    })
    this.selectComponent("#widgetsshare").showSharefunc();

  },
  onChangeNav(e){
    console.log("e")
    console.log(e)
    this.setData({
      status:e.detail.name
    })
    var that=this
    this.getpagelist(that,1)
  },
  getpagelist(that, rest, strwhere) {
    if (rest == 1) {
      if (!strwhere) {
        strwhere = {};
      }

      strwhere["status"] = this.data.status;
      strwhere["seachval"] = this.data.searchkey;

      that.setData({
        modellist: []
      });
    }
    actbill.offset(that, rest, strwhere).then(function (result) {
      var updatemdoel = {};

      if ((result["code"] == "over" && result["obj"]) || result["code"] == "loadmore") {

        for (var a in result["obj"]["modellist"]) {
          if (result["obj"]["modellist"][a]) {
            result["obj"]["modellist"][a]["snapshot"] = JSON.parse(result["obj"]["modellist"][a]["snapshot"])
          }
        }
        if (rest == 1) {
          updatemdoel["modellist"] = result["obj"]["modellist"];
        } else {
          if (result["obj"]["modellist"].length > 0) {
            updatemdoel["modellist"] = that.data.modellist.concat(result["obj"]["modellist"]);
          }
        }
      }
      wx.stopPullDownRefresh();
      that.setData(updatemdoel);


    });
  },
  onShow(){
    var that=this;
    this.getpagelist(that,1)
  },
  afterlogin: function (options, config, userinfo) {
    if (userinfo) {
      this.setData({
        member: userinfo
      })
    }
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this
    this.getpagelist(that, -1)
  }
})